<?php
require 'Model.php';
class UserModel extends Model{

function getUserList(){
	$sql="select * from users";
	$data=$this->conn->query($sql);
	return $data;
}
function delUser($uid){
	$sql="delete from users where uid=$uid";
	$result=$this->conn->query($sql);
	return $result;

}
function adduser($data,$file){
//文件上传
$arr=explode('.', $file['pic']['name']);
$suffix=$arr[count($arr)-1];
$newname=date("YmdHis").rand(100,999).'.'.$suffix;
$filepath='./photos/';
	
move_uploaded_file($file['pic']['tmp_name'], $filepath.$newname);
//把数据插入数据库
$uname=$data['username'];
$password=$data['password'];
$gender=$data['gender'];
$birthdate=$data['birthdate'];
$hobby=implode($data['hobby'], ',');
$degree=$data['degree'];
$intro=$data['intro'];
$pic=$newname;
$sql="insert into users values(null,'$uname',md5('$password'),$gender,'$birthdate','$hobby',$degree,'$intro','$pic')";
$result=$this->conn->query($sql);
	return $result;
}
function edituser($uid){
$sql="select * from users where uid=$uid";
$data=$this->conn->query($sql);
return $data;
}

function updateuser($data,$file){
//接受数据
	$uid=$data['uid'];
	$username=$data['username'];
	$gender=$data['gender'];
	$birthdate=$data['birthdate'];
	$hobby=implode($data['hobby'], ',');
	$degree=$data['degree'];
	$intro=$data['intro'];

//如果修改了头像，需要上传新头像，否则原头像不动
if ($file['pic']['size']==0) {
	$sql = "update users set uname='$username', birthdate='$birthdate', gender=$gender, hobby='$hobby', degree=$degree,intro='$intro' where uid=$uid";	
}
else{
//上传头像并且更新根据uid更新数据
$arr=explode('.', $file['pic']['name']);
$suffix=$arr[count($arr)-1];
$newname=date("YmdHis").rand(100,999).'.'.$suffix;
$filepath='./photos/';
	
move_uploaded_file($file['pic']['tmp_name'], $filepath.$newname);	
$sql = "update users set uname='$username', birthdate='$birthdate', gender=$gender, hobby='$hobby', degree=$degree,intro='$intro',pic='$newname' where uid=$uid";	

}
echo $sql;
$result=$this->conn->query($sql);
	return $result;
}
}
 ?>
